package net.xiaosaguo.mymall.search.controller;

import lombok.extern.slf4j.Slf4j;
import net.xiaosaguo.common.exception.BizCodeEnum;
import net.xiaosaguo.common.pojo.dto.es.SkuEsModel;
import net.xiaosaguo.common.utils.R;
import net.xiaosaguo.mymall.search.service.ProductSaveService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * description: Elasticsearch 保存
 *
 * @author xiaosaguo
 * @date 2020/06/20
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticsearchSaveController {

    @Resource
    ProductSaveService productSaveService;

    /**
     * description: 上架商品
     *
     * @param skuEsModels 要上架的 sku
     * @author xiaosaguo
     * @date 2020/06/20 05:29
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean b;
        try {
            b = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("Elasticsearch商品上架错误：", e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }

        if (b) {
            return R.ok();
        } else {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
    }
}
